package com.example.demo.util;

import java.util.*;
import java.util.stream.Collectors;

/**
 * #字体颜色：30m-37m 黑、红、绿、黄、蓝、紫、青、白
 * echo -e "\033[30m ${str}\033[0m"      ## 黑色字体
 * echo -e "\033[31m ${str}\033[0m"      ## 红色
 * echo -e "\033[32m ${str}\033[0m"      ## 绿色
 * echo -e "\033[33m ${str}\033[0m"      ## 黄色
 * echo -e "\033[34m ${str}\033[0m"      ## 蓝色
 * echo -e "\033[35m ${str}\033[0m"      ## 紫色
 * echo -e "\033[36m ${str}\033[0m"      ## 青色
 * echo -e "\033[37m ${str}\033[0m"      ## 白色
 * <p>
 * #背景颜色：40-47 黑、红、绿、黄、蓝、紫、青、白
 * echo -e "\033[41;37m ${str} \033[0m"     ## 红色背景色，白色字体
 * echo -e "\033[41;33m ${str} \033[0m"     ## 红底黄字
 * echo -e "\033[1;41;33m ${str} \033[0m"   ## 红底黄字 高亮加粗显示
 * echo -e "\033[5;41;33m ${str} \033[0m"   ## 红底黄字 字体闪烁显示
 * echo -e "\033[47;30m ${str} \033[0m"     ## 白底黑字
 * echo -e "\033[40;37m ${str} \033[0m"     ## 黑底白字
 * <p>
 * #其他参数说明
 * 　　\033[1;m 设置高亮加粗
 * 　　\033[4;m 下划线
 * 　　\033[5;m 闪烁
 */
public class ColorPrint {
    interface Code {
        int getCode();
    }

    private enum FontCodeCode implements Code {
        // 字体颜色
        black(30), red(31), green(32), yellow(33), blue(34), purple(35), cyan_blue(36), white(37);
        public int code;

        FontCodeCode(int code) {
            this.code = code;
        }

        @Override
        public int getCode() {
            return this.code;
        }
    }

    private enum BackgroundCodeCode implements Code {
        // 背景颜色
        black(40), red(41), green(42), yellow(43), blue(44), purple(45), cyan_blue(46), white(47);
        public int code;

        BackgroundCodeCode(int code) {
            this.code = code;
        }

        @Override
        public int getCode() {
            return this.code;
        }
    }

    private enum OtherCode implements Code {
        // 高亮、下划线、闪烁
        high_light(1), underscore(4), flicker(5);

        public int code;

        OtherCode(int code) {
            this.code = code;
        }

        @Override
        public int getCode() {
            return this.code;
        }
    }

    private static final Random random = new Random();

    public static void main(String[] args) {
    }

    public static String strColorWrapper(String s) {
        FontCodeCode[] values  = FontCodeCode.values();
        StringBuilder  builder = new StringBuilder();
        for (char c : s.toCharArray()) {
            String s1 = strColorWrapper(c + "", values[random.nextInt(values.length)], null);
            builder.append(s1);
        }
        return builder.toString();
    }


    private static String strColorWrapper(String s, FontCodeCode fontColorCode, BackgroundCodeCode backgroundColorCode, OtherCode... otherCode) {
        if (fontColorCode != null || backgroundColorCode != null || otherCode != null) {

            List<Code> enums = new ArrayList<>(2);
            if (fontColorCode != null) {
                enums.add(fontColorCode);
            }
            if (backgroundColorCode != null) {
                enums.add(backgroundColorCode);
            }
            if (otherCode != null && otherCode.length > 0) {
                for (OtherCode code : otherCode) {
                    if (code == null) {
                        continue;
                    }
                    enums.add(code);
                }
            }
            StringBuilder m = new StringBuilder();

            String cs = enums.stream().map(anEnum -> String.valueOf(anEnum.getCode())).collect(Collectors.joining(";"));
            m.append("\033[").append(cs).append('m').append(s).append("\033[0m");
            s = m.toString();
        }
        return s;
    }
}
